package com.lcx.plugin.neteaseCloud.component.toolwindow;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.ui.JBUI;
import com.lcx.plugin.neteaseCloud.component.gd.MyDefaultTreeCellRenderer;
import com.lcx.plugin.neteaseCloud.component.gd.SongDetailDialogWrapper;
import com.lcx.plugin.neteaseCloud.component.gd.SongListDialogWrapper;
import com.lcx.plugin.neteaseCloud.component.handle.SongPlayHandle;
import com.lcx.plugin.neteaseCloud.component.handle.SongQueueHandle;
import com.lcx.plugin.neteaseCloud.component.handle.UserHandle;
import com.lcx.plugin.neteaseCloud.component.line.BottomLine;
import com.lcx.plugin.neteaseCloud.component.toolwindow.login.LoginTypeDialogWrapper;
import com.lcx.plugin.neteaseCloud.component.toolwindow.login.LogoutDialogWrapper;
import com.lcx.plugin.neteaseCloud.constant.IconConstant;
import com.lcx.plugin.neteaseCloud.music.DataCenter;
import com.lcx.plugin.neteaseCloud.music.api.MusicListApi;
import com.lcx.plugin.neteaseCloud.music.entity.MusicListEntity;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;

/**
 * @author daxiong
 * @date 2021/9/10 下午10:28
 */
public class MainPanel {

    private JPanel contentPanel;
    private Border lineBorder = new LineBorder(Color.BLACK);
    // --------- 我的面板 -----------
    private JPanel myPanel;
    private ImagePanel avatarPanel;
    private JLabel nameLabel;
    private ImagePanel bgPanel;
    // --------- 歌单面板 -----------
    private JPanel musicListPanel;
    private JScrollPane songListPanel;

    // --------- 队列面板 -----------
    private JPanel queuePanel;
    private JScrollPane queuePlayScrollPane;
    private JLabel repeatLabel;

    // --------- 播放面板 -----------
    private JPanel playPanel;
    private JLabel playLabel;

    //  --------- 歌词面板 -----------
    private JLabel lyricBefore;
    private JLabel lyricNow;
    private JLabel lyricAfter;
    /**
     * 歌曲封面标签
     */
    private JLabel songAvatarLabel;
    /**
     * 歌曲封面panel
     */
    private ImagePanel songAvatarPanel;
    /**
     * 歌曲名称标签
     */
    private JLabel songNameLabel;
    /**
     * 歌手名称标签
     */
    private JLabel singerNameLabel;

    /**
     * 我的面板的纵坐标
     */
    private static final int COMMON_WIDTH = 350;
    /**
     * 我的面板的纵坐标
     */
    private static final int MY_Y = 0;
    /**
     * 我的面板的高度
     */
    private static final int MY_HEIGHT = 130;
    /**
     * 歌单的纵坐标
     */
    private static final int MUSIC_LIST_Y = MY_Y + MY_HEIGHT + 10;
    /**
     * 歌单的高度
     */
    private static final int MUSIC_LIST_HEIGHT = 250;
    /**
     * 队列的纵坐标
     */
    private static final int QUEUE_Y = MUSIC_LIST_Y + MUSIC_LIST_HEIGHT + 10;
    /**
     * 队列的高度
     */
    private static final int QUEUE_HEIGHT = 250;
    /**
     * 歌曲播放面板的纵坐标
     */
    private static final int SONG_PLAY_Y = QUEUE_Y + QUEUE_HEIGHT + 10;
    /**
     * 歌曲播放面板的高度
     */
    private static final int SONG_PLAY_HEIGHT = 90;
    /**
     * 歌手名称标签
     */
    private JPanel lyricPanel;
    /**
     * 歌曲播放面板的纵坐标
     */
    private static final int LYRIC_Y = SONG_PLAY_Y + SONG_PLAY_HEIGHT + 10;
    /**
     * 歌曲播放面板的高度
     */
    private static final int LYRIC_HEIGHT = 100;

    public JComponent getContent() {
        return contentPanel;
    }

    /**
     * Create the frame.
     */
    public MainPanel() {
        contentPanel = new JPanel();
        contentPanel.setBorder(JBUI.Borders.empty(5));
        contentPanel.setLayout(null);

        // "我的"面板
        initMyPanel();
        // "歌单"面板
        initMusicListPanel();
        // "队列"面板
        initQueuePanel();
        // 歌曲播放面板
        initMusicPlayPanel();
        // 歌词面板
        initLyricPanel();

        // 尝试本地自动登录
        if (UserHandle.userFile.exists()) {
            String content = FileUtil.readString(UserHandle.userFile, StandardCharsets.UTF_8);
            Map map = JSON.parseObject(content, Map.class);
            DataCenter.map2Attr(map);
            try {
                setMyPanel();
                setMusicListPanel();
                setQueuePanel();
                setPlayPanel();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }

    }

    /**
     * 初始化歌词面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/30 下午2:07
     */
    private void initLyricPanel() {
        lyricPanel = new JPanel();
        lyricPanel.setLayout(null);
        lyricPanel.setBounds(0, LYRIC_Y, COMMON_WIDTH, LYRIC_HEIGHT);

        // 字体
        Font plain = new Font("宋体", Font.PLAIN, 13);
        Font bold = new Font("宋体", Font.BOLD, 16);
        // 上一句
        lyricBefore = new JLabel();
        lyricBefore.setBounds(15, 5, 300, 30);
        lyricBefore.setFont(plain);
        lyricBefore.setForeground(Color.GRAY);
        lyricPanel.add(lyricBefore);
        // 当前句
        lyricNow = new JLabel();
        lyricNow.setBounds(15, 37, 300, 30);
        lyricNow.setFont(bold);
        lyricNow.setForeground(Color.MAGENTA);
        lyricPanel.add(lyricNow);
        // 下一句
        lyricAfter = new JLabel();
        lyricAfter.setFont(plain);
        lyricAfter.setForeground(Color.GRAY);
        lyricAfter.setBounds(15, 69, 300, 30);
        lyricPanel.add(lyricAfter);

        contentPanel.add(lyricPanel);
    }

    /**
     * 设置歌词面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/30 下午4:03
     */
    public void setLyric() {
        String textBefore = "";
        if (DataCenter.showLyricList.size() > 0) {
            textBefore = DataCenter.showLyricList.get(0).getLyric();
        }
        lyricBefore.setText(textBefore);
        String textNow = "";
        if (DataCenter.showLyricList.size() > 1) {
            textNow = DataCenter.showLyricList.get(1).getLyric();
        }
        lyricNow.setText(textNow);
        String textAfter = "";
        if (DataCenter.showLyricList.size() > 2) {
            textAfter = DataCenter.showLyricList.get(2).getLyric();
        }
        lyricAfter.setText(textAfter);
    }

    /**
     * 初始化歌曲播放面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/9/18 下午1:30
     */
    private void initMusicPlayPanel() {
        playPanel = new JPanel();
        playPanel.setLayout(null);
        contentPanel.add(playPanel);
        playPanel.setBounds(0, SONG_PLAY_Y, COMMON_WIDTH, SONG_PLAY_HEIGHT);

        // 顶部实线
        playPanel.add(BottomLine.getLine(0, 3, COMMON_WIDTH));

        // 播放界面
        // 上一首
        JLabel beforeLabel = new JLabel();
        beforeLabel.setIcon(IconConstant.BEFORE);
        beforeLabel.setBounds(100, 55, 40, 40);
        playPanel.add(beforeLabel);
        // 播放
        playLabel = new JLabel();
        playLabel.setIcon(IconConstant.PLAY);
        playLabel.setBounds(170, 55, 40, 40);
        playPanel.add(playLabel);
        // 下一首
        JLabel afterLabel = new JLabel();
        afterLabel.setIcon(IconConstant.AFTER);
        afterLabel.setBounds(240, 55, 40, 40);
        afterLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                // 播放下一首歌曲
                SongPlayHandle.playNextWithNoMode();
            }
        });
        playPanel.add(afterLabel);
        // 横线
        JLabel lineLabel = new JLabel();
        lineLabel.setBorder(lineBorder);
        lineLabel.setBounds(100, 58, 200, 1);
        playPanel.add(lineLabel);
        // 左侧歌曲封面界面
        songAvatarLabel = new JLabel();
        songAvatarLabel.setBounds(5, 5, 80, 80);
        songAvatarLabel.setBorder(lineBorder);
        playPanel.add(songAvatarLabel);
        // 歌曲名展示界面
        songNameLabel = new JLabel("—— ——");
        songNameLabel.setBounds(100, 5, 200, 25);
        playPanel.add(songNameLabel);
        // 歌手展示界面
        singerNameLabel = new JLabel("—— ——");
        singerNameLabel.setBounds(100, 35, 200, 20);
        playPanel.add(singerNameLabel);

    }

    /**
     * 初始化"歌单"面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/9/16 上午11:32
     */
    private void initMusicListPanel() {
        musicListPanel = new JPanel();
        musicListPanel.setLayout(null);
        contentPanel.add(musicListPanel);
        musicListPanel.setBounds(0, MUSIC_LIST_Y, COMMON_WIDTH, MUSIC_LIST_HEIGHT);

        // 顶部下划线
        musicListPanel.add(BottomLine.getLine(0, 3, COMMON_WIDTH));

        // 歌单
        JLabel musicListLabel = new JLabel("歌单");
        musicListLabel.setBounds(5, 5, 50, 20);
        musicListPanel.add(musicListLabel);

    }

    /**
     * 创建歌单树形结构
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/9/18 上午11:20
     */
    public void setMusicListTree() {
        MusicListEntity rootEntity = new MusicListEntity(DataCenter.uid, DataCenter.userProfile.getString("nickname"), 0);
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootEntity);

        DefaultMutableTreeNode node;
        DefaultMutableTreeNode childNode;
        for (Object o : DataCenter.musicList) {
            JSONObject item = (JSONObject) o;
            MusicListEntity musicListEntity = new MusicListEntity(item.getString("id"), item.getString("name"), 1);
            node = new DefaultMutableTreeNode(musicListEntity);
            MusicListEntity entity = new MusicListEntity("", "", 2);
            childNode = new DefaultMutableTreeNode(entity);
            node.add(childNode);
            root.add(node);
        }

        Tree musicListTree = new Tree(root);
        musicListTree.setBounds(0, 0, 300, 200);
        musicListTree.collapseRow(0);

        // 设置为单击打开
        musicListTree.setToggleClickCount(1);
        beautifyTree(musicListTree);

        musicListTree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                TreePath selectionPath = musicListTree.getSelectionPath();
                if (selectionPath == null) {
                    return;
                }
                DefaultMutableTreeNode clickNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
                MusicListEntity musicListEntity = (MusicListEntity) clickNode.getUserObject();

                if (Objects.equals(e.getButton(), MouseEvent.BUTTON1)) {
                    // 左键不处理
                } else if (Objects.equals(e.getButton(), MouseEvent.BUTTON3)) {
                    // 右键
                    if (musicListEntity.getLevel() == 2) {
                        // 叶子节点
                        new SongDetailDialogWrapper(musicListEntity).show();
                    } else if (musicListEntity.getLevel() == 1) {
                        // 歌单节点
                        new SongListDialogWrapper(musicListEntity).show();
                    }
                }
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
            }
        });

        // 添加展开关闭监听器
        musicListTree.addTreeExpansionListener(new TreeExpansionListener() {
            @Override
            public void treeExpanded(TreeExpansionEvent event) {
                TreePath path = event.getPath();
                DefaultMutableTreeNode clickNode = (DefaultMutableTreeNode) path.getLastPathComponent();
                MusicListEntity musicListEntity = (MusicListEntity) clickNode.getUserObject();
                treeSetDetail(clickNode, musicListEntity);
                musicListTree.updateUI();
            }

            @Override
            public void treeCollapsed(TreeExpansionEvent event) {
                musicListTree.collapsePath(event.getPath());
            }
        });

        songListPanel = new JScrollPane(musicListTree);
        // 将树添加到滚动面板上
        songListPanel.setBorder(null);
        songListPanel.setBounds(15, 30, 300, 220);
        songListPanel.setOpaque(false);
        musicListPanel.add(songListPanel);

        musicListPanel.updateUI();
    }

    /**
     * 清除歌单面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 下午2:44
     */
    public void clearMusicPanel() {
        if (Objects.nonNull(songListPanel)) {
            musicListPanel.remove(songListPanel);
        }
        musicListPanel.updateUI();
    }

    /**
     * 为歌单 添加音乐子节点
     *
     * @param clickNode
     * @param musicListEntity
     * @return void
     * @author daxiong
     * @date 2021/9/17 下午2:05
     */
    private void treeSetDetail(DefaultMutableTreeNode clickNode, MusicListEntity musicListEntity) {
        // 移除之前所有的子节点
        if (musicListEntity.getLevel() == 1) {
            clickNode.removeAllChildren();
            // 歌单节点，则获取歌单详情
            JSONArray musicListDetail = MusicListApi.getMusicListDetail(musicListEntity.getId());
            DefaultMutableTreeNode newChild;
            if (Objects.nonNull(musicListDetail)) {
                for (Object detail : musicListDetail) {
                    JSONObject detailObj = (JSONObject) detail;
                    MusicListEntity entity = SongQueueHandle.transfer2MusicEntity(musicListEntity, detailObj);
                    newChild = new DefaultMutableTreeNode(entity);
                    clickNode.add(newChild);
                }
            }
        }
    }

    public void beautifyTree(Tree tree) {
        // 首先，创建一个树节点的渲染器
        MyDefaultTreeCellRenderer render = new MyDefaultTreeCellRenderer();
        // 最后，设置树组件的节点渲染器
        tree.setCellRenderer(render);
    }

    /**
     * 初始化"队列"面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/9/16 上午11:32
     */
    private void initQueuePanel() {
        queuePanel = new JPanel();
        queuePanel.setLayout(null);
        contentPanel.add(queuePanel);
        queuePanel.setBounds(0, QUEUE_Y, COMMON_WIDTH, QUEUE_HEIGHT);
        // 顶部线
        queuePanel.add(BottomLine.getLine(0, 3, COMMON_WIDTH));
        // 队列
        JLabel queueLabel = new JLabel("队列");
        queueLabel.setBounds(5, 5, 50, 20);
        queuePanel.add(queueLabel);
        // 随机打乱按钮
        JLabel randomLabel = new JLabel();
        randomLabel.setIcon(IconConstant.RANDOM);
        randomLabel.setBounds(235, 7, 16, 16);
        randomLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                randomLabel.setIcon(IconConstant.RANDOM);
                SongQueueHandle.randomQueue();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                randomLabel.setIcon(IconConstant.RANDOM_SELECT);
            }
        });
        queuePanel.add(randomLabel);
        // 单曲循环按钮
        repeatLabel = new JLabel();
        repeatLabel.setIcon(IconConstant.REPEAT);
        repeatLabel.setBounds(265, 7, 20, 16);
        repeatLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (DataCenter.playMode == 0) {
                    // 单曲循环
                    SongQueueHandle.repeat();
                    repeatLabel.setIcon(IconConstant.REPEAT_SELECT);
                } else if (DataCenter.playMode == 1) {
                    // 取消单曲循环
                    SongQueueHandle.cancelRepeat();
                    repeatLabel.setIcon(IconConstant.REPEAT);
                }
            }
        });
        queuePanel.add(repeatLabel);
        // 删除队列按钮
        JLabel deleteLabel = new JLabel();
        deleteLabel.setIcon(IconConstant.DELETE);
        deleteLabel.setBounds(295, 7, 16, 16);
        deleteLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                deleteLabel.setIcon(IconConstant.DELETE);
                MusicListEntity playingSong = DataCenter.queueList.get(0);
                DataCenter.queueList.clear();
                DataCenter.queueList.add(playingSong);
                setQueuePanel();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                deleteLabel.setIcon(IconConstant.DELETE_SELECT);
            }
        });
        queuePanel.add(deleteLabel);
        // 队列详情滚动面板
        queuePlayScrollPane = new JScrollPane();
        queuePlayScrollPane.setBorder(null);
        queuePlayScrollPane.setBounds(15, 30, 300, 210);
        queuePanel.add(queuePlayScrollPane);

    }

    /**
     * 初始化"我的"面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/9/16 上午11:31
     */
    private void initMyPanel() {
        myPanel = new JPanel();
        myPanel.setLayout(null);
        contentPanel.add(myPanel);
        myPanel.setBounds(0, MY_Y, COMMON_WIDTH, MY_HEIGHT);
        // 账号
        JLabel accountLabel = new JLabel("账号");
        accountLabel.setBounds(5, 5, 50, 20);
        myPanel.add(accountLabel);
        // 登录按钮
        JLabel loginImageLabel = new JLabel();
        loginImageLabel.setIcon(IconConstant.LOGIN_ICON);
        loginImageLabel.setBounds(265, 7, 16, 16);
        loginImageLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                new LoginTypeDialogWrapper().show();
            }
        });
        myPanel.add(loginImageLabel);
        // 登出按钮
        JLabel logoutImageLabel = new JLabel();
        logoutImageLabel.setIcon(IconConstant.LOGOUT);
        logoutImageLabel.setBounds(295, 7, 16, 16);
        logoutImageLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                new LogoutDialogWrapper().show();
            }
        });
        myPanel.add(logoutImageLabel);
    }


    /**
     * 登录后给"我的"面板赋值
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/9/16 上午11:31
     */
    public void setMyPanel() throws IOException {
        JSONObject userProfile = DataCenter.userProfile;
        if (Objects.nonNull(userProfile)) {
            // 用户头像
            String avatarUrl = userProfile.getString("avatarUrl");
            if (StrUtil.isNotBlank(avatarUrl)) {
                URL url = new URL(avatarUrl);
                avatarPanel = new ImagePanel(
                        new ImageIcon(ImageIO.read(url)),
                        new CustomRectangle(0, 0, 70, 70));
                avatarPanel.setBounds(30, 45, 70, 70);
                myPanel.add(avatarPanel);
            }

            // 用户名展示
            String nickname = userProfile.getString("nickname");
            if (StrUtil.isNotBlank(nickname)) {
                nameLabel = new JLabel(nickname);
                nameLabel.setBounds(130, 60, 200, 30);
                // 创建1个字体实例
                Font font = new Font("宋体", Font.PLAIN, 22);
                nameLabel.setFont(font);
                nameLabel.setForeground(Color.white);
                myPanel.add(nameLabel);
            }

            // 背景图片面板
            String backgroundUrl = userProfile.getString("backgroundUrl");
            if (StrUtil.isNotBlank(backgroundUrl)) {
                URL bgUrl = new URL(backgroundUrl);
                bgPanel = new ImagePanel(
                        new ImageIcon(ImageIO.read(bgUrl)),
                        new CustomRectangle(0, 0, 300, 90));
                bgPanel.setBounds(10, 33, 300, 90);
                myPanel.add(bgPanel);
            }
        }
        myPanel.updateUI();
    }

    public void clearMyPanel() {
        if (Objects.nonNull(avatarPanel)) {
            myPanel.remove(avatarPanel);
        }
        if (Objects.nonNull(nameLabel)) {
            myPanel.remove(nameLabel);
        }
        if (Objects.nonNull(bgPanel)) {
            myPanel.remove(bgPanel);
        }
        myPanel.updateUI();
    }

    public void setPlayPanel() throws IOException {
        MusicListEntity playingSongEntity = DataCenter.playingSongEntity;
        if (Objects.nonNull(playingSongEntity)) {
            // 正在播放歌曲
            String avatarUrl = playingSongEntity.getSongAvatar();
            if (StrUtil.isNotBlank(avatarUrl)) {
                URL url = new URL(avatarUrl);
                songAvatarPanel = new ImagePanel(
                        new ImageIcon(ImageIO.read(url)),
                        new CustomRectangle(0, 0, 80, 80));
                songAvatarPanel.setBounds(0, 0, 80, 80);
                // 先移除掉之前的
                songAvatarLabel.removeAll();
                songAvatarLabel.add(songAvatarPanel);
            }

            // 歌曲名展示
            String songName = playingSongEntity.getSongName();
            if (StrUtil.isNotBlank(songName)) {
                // 创建1个字体实例
                Font font = new Font("宋体", Font.PLAIN, 22);
                songNameLabel.setFont(font);
                songNameLabel.setText(songName);
            }
            // 歌手展示
            String singerName = playingSongEntity.getAuthorName();
            String albumName = playingSongEntity.getAlbumName();
            if (StrUtil.isNotBlank(singerName)) {
                // 创建1个字体实例
                Font font = new Font("宋体", Font.PLAIN, 19);
                singerNameLabel.setFont(font);
                singerNameLabel.setText(singerName + " - " + albumName);
            }
            // 修改播放按钮为暂停按钮
            setPause();
        }
        playPanel.updateUI();
    }

    /**
     * 清除播放面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 下午2:46
     */
    public void clearPlayPanel() {
        // 停止播放
        SongPlayHandle.stop();
        // 清空面板
        songAvatarLabel.removeAll();
        songNameLabel.setText("—— ——");
        singerNameLabel.setText("—— ——");
        playLabel.setIcon(IconConstant.PLAY);
        playPanel.updateUI();
    }

    /**
     * 设置队列面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/15 下午4:34
     */
    public void setQueuePanel() {
        // 队列详情面板
        String[] names = DataCenter.queueList.stream().map(MusicListEntity::getSongName).toArray(String[]::new);
        JList<String> queuePlayList = new JList<>(names);
        Icon[] icons = {IconConstant.MUSIC_ITEM};
        Icon[] selectIcons = {IconConstant.MUSIC_ITEM_SELECT};

        queuePlayList.setCellRenderer(new CustomNodeRenderer(icons, selectIcons));

        queuePlayList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (e.getClickCount() == 2) {
                    int selectedIndex = queuePlayList.getSelectedIndex();
                    MusicListEntity musicListEntity = DataCenter.queueList.remove(selectedIndex);
                    if (Objects.equals(DataCenter.playingSongEntity, musicListEntity)
                            && selectedIndex == 0) {
                        DataCenter.queueList.add(0, musicListEntity);
                        SongPlayHandle.playAgain();
                    } else {
                        DataCenter.queueList.add(1, musicListEntity);
                        SongPlayHandle.playNextWithNoMode();
                    }
                }
            }
        });

        // 先移除再添加
        queuePanel.remove(queuePlayScrollPane);
        queuePlayScrollPane = new JScrollPane(queuePlayList);
        queuePlayScrollPane.setBounds(15, 30, 300, 210);
        queuePlayScrollPane.setBorder(null);
        queuePanel.add(queuePlayScrollPane);
        queuePanel.updateUI();
    }

    /**
     * 清除队列面板
     *
     * @param
     * @return void
     * @author daxiong
     * @date 2021/10/20 下午2:45
     */
    public void clearQueuePanel() {
        if (Objects.nonNull(queuePlayScrollPane)) {
            queuePanel.remove(queuePlayScrollPane);
        }
        repeatLabel.setIcon(IconConstant.REPEAT);
        queuePanel.updateUI();
    }

    public void setPause() {
        // 先移除掉之前的图标
        playPanel.remove(playLabel);

        playLabel = new JLabel();
        playLabel.setIcon(IconConstant.PAUSE);
        playLabel.setBounds(170, 55, 40, 40);
        playLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                setPlaying();
            }
        });
        playPanel.add(playLabel);
        playPanel.updateUI();
        SongPlayHandle.pause();
    }

    public void setPlaying() {
        // 先移除掉之前的图标
        playPanel.remove(playLabel);

        playLabel = new JLabel();
        playLabel.setIcon(IconConstant.PLAY);
        playLabel.setBounds(170, 55, 40, 40);
        playLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                setPause();
            }
        });
        playPanel.add(playLabel);
        playPanel.updateUI();
        SongPlayHandle.pause();
    }


    public void setMusicListPanel() {
        setMusicListTree();
    }

}
